package com.xingzhi.shortvideosharingplatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingzhi.shortvideosharingplatform.mapper.VideoZMapper;
import com.xingzhi.shortvideosharingplatform.model.Video;
import com.xingzhi.shortvideosharingplatform.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class VideoZServiceImpl extends ServiceImpl<VideoZMapper, Video> implements VideoService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean incrementLikeCount(Long videoId) {
        try {
            return baseMapper.incrementLikeCount(videoId, 1) > 0;
        } catch (Exception e) {
            log.error("增加视频点赞数失败: videoId={}", videoId, e);
            return false;
        }
    }

    @Override
    public boolean decrementLikeCount(Long videoId) {
        try {
            return baseMapper.incrementLikeCount(videoId, -1) > 0;
        } catch (Exception e) {
            log.error("减少视频点赞数失败: videoId={}", videoId, e);
            return false;
        }
    }

    @Override
    public Long getVideoAuthorId(Long videoId) {
        try {
            Video video = getById(videoId);
            return video != null ? video.getUserId() : null;
        } catch (Exception e) {
            log.error("获取视频作者ID失败: videoId={}", videoId, e);
            return null;
        }
    }

    @Override
    public Video getVideoById(Long id) {
        return baseMapper.selectById(id);
    }
    
    @Override
    public boolean incrementAuthorLikeCount(Long videoId) {
        try {
            Long authorId = getVideoAuthorId(videoId);
            if (authorId == null) {
                return false;
            }
            
            // 通过SQL更新user_profiles表中的like_count
            String sql = "UPDATE user_profiles SET like_count = like_count + 1 WHERE user_id = ?";
            int affected = jdbcTemplate.update(sql, authorId);
            return affected > 0;
        } catch (Exception e) {
            log.error("增加视频作者总点赞数失败: videoId={}", videoId, e);
            return false;
        }
    }
    
    @Override
    public boolean decrementAuthorLikeCount(Long videoId) {
        try {
            Long authorId = getVideoAuthorId(videoId);
            if (authorId == null) {
                return false;
            }
            
            // 通过SQL更新user_profiles表中的like_count，确保不小于0
            String sql = "UPDATE user_profiles SET like_count = GREATEST(like_count - 1, 0) WHERE user_id = ?";
            int affected = jdbcTemplate.update(sql, authorId);
            return affected > 0;
        } catch (Exception e) {
            log.error("减少视频作者总点赞数失败: videoId={}", videoId, e);
            return false;
        }
    }
    
    @Override
    public boolean incrementFavoriteCount(Long videoId) {
        try {
            return baseMapper.incrementFavoriteCount(videoId, 1) > 0;
        } catch (Exception e) {
            log.error("增加视频收藏数失败: videoId={}", videoId, e);
            return false;
        }
    }
    
    @Override
    public boolean decrementFavoriteCount(Long videoId) {
        try {
            return baseMapper.incrementFavoriteCount(videoId, -1) > 0;
        } catch (Exception e) {
            log.error("减少视频收藏数失败: videoId={}", videoId, e);
            return false;
        }
    }
} 